package com.wizz.trade.information.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.dto.CompanydeptDto;
import com.wizz.trade.information.dto.page.CompanydeptPageDto;
import com.wizz.trade.information.entity.Companydept;
import com.wizz.trade.information.service.CompanydeptService;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.information.vo.CompanydeptVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资料模块/公司部门
 */
@RestController
@RequestMapping("/companydept")
@Slf4j
public class CompanydeptController {

    @Resource
    private CompanydeptService companydeptService;

    /**
     * 添加操作
     *
     * @param companydeptDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult addCompanydept(@RequestBody CompanydeptDto companydeptDto) {
        Companydept companydept = new Companydept();
        BeanUtils.copyProperties(companydeptDto, companydept);
        companydeptService.addCompanydept(companydept);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 更新（编辑）操作
     *
     * @param companydeptDto
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult updateCompanydept(@RequestBody CompanydeptDto companydeptDto, @PathVariable("id") Integer id) {
        Companydept companydept = new Companydept();
        BeanUtils.copyProperties(companydeptDto, companydept);
        companydept.setId(id);
        companydeptService.updateCompanydept(companydept);
        return new ResponseResult<>(StatusCode.OK, "修改成功");
    }


    /**
     * 删除（撤销）操作
     *
     * @param id
     * @return
     */

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult deleteCompanydept(@PathVariable("id") Integer id) {
        companydeptService.deleteCompanydept(id);
        return new ResponseResult<>(StatusCode.OK, "删除成功");
    }


    /**
     * 分页刷新、条件查询
     *
     * @param companydeptPageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('基础信息操作')")
    public ResponseResult<PageVo<CompanydeptVo>> list(CompanydeptPageDto companydeptPageDto) {
        PageVo<Companydept> rawPage = companydeptService.getPage(companydeptPageDto);
        List<Companydept> data = rawPage.getData();
        List<CompanydeptVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            CompanydeptVo companydeptVoVo = new CompanydeptVo();
            BeanUtils.copyProperties(data.get(i), companydeptVoVo);
            dataVo.add(companydeptVoVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }

    /**
     * 获取本公司部门的下拉框选项
     * @param companyId 公司id
     * @return 选项
     */
    @GetMapping("/options/1/{id}")
    public ResponseResult<List<Map<Integer, String>>> getOurDeptOptions(@PathVariable("id") Integer companyId){
        return new ResponseResult<>(StatusCode.OK, companydeptService.getOurDeptOptions(companyId));
    }

    /**
     * 获取其他公司部门的下拉框选项
     * @param companyId 公司id
     * @return 选项
     */
    @GetMapping("/options/2/{id}")
    public ResponseResult<List<Map<Integer, String>>> getCompanyDeptOptions(@PathVariable("id") Integer companyId){
        return new ResponseResult<>(StatusCode.OK, companydeptService.getCompanyDeptOptions(companyId));
    }
}
